package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.utils.StringUtils;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.ProductDetailConvert;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ProjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <h1> ProductServiceImpl </h1>
 * Created by GaoJia
 * Date  2021/11/1 22:43
 */
@Slf4j
@Service
public class ProductServiceImpl implements IProductService {

    // 一、注入相关mapper 以及 convert
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;


    // 三、实现每一个具体的方法
    // 首先进行参数校验
    // 然后进行
    // 异常处理
    // 最后返回 对应的 Response 数据

    /**
     * <h2> 查看商品详情 </h2>
     * Created by GaoJia
     */
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        log.info("调用了getProductDetail，查看商品详情");
        ProductDetailResponse response = new ProductDetailResponse();

        try {
            request.requestCheck();// 进行参数校验
            Long itemId = request.getId();
            Item item = itemMapper.selectByPrimaryKey(itemId);

            // 往 response 里面加入值 放入对应的 常量值
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

            // 将数据 转换为需要的形式 使用 ProductDetailConvert
            ProductDetailDto productDetailDto = ProductDetailConvert.item2DetailDto(item);

            // 从 `tb_item_desc` 中查到之后 获取 detail 赋值给 productDetailDto
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
            productDetailDto.setDetail(itemDesc.getItemDesc());
            response.setProductDetailDto(productDetailDto);
        } catch (Exception ee) {
            // 如果发生了异常那么就 往数据中塞入 DB 异常信息
            ee.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return response;
    }

    /**
     * <h2> 分页查询商品列表 </h2>
     * Created by GaoJia
     */
    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        log.info("调用 getAllProduct-1st 进行 分页查询商品列表");
        AllProductResponse response = new AllProductResponse();

        try {
            request.requestCheck();
            // 使用分页助手进行分页
            PageHelper.startPage(request.getPage(), request.getSize());

            String sort = request.getSort();
            if (StringUtils.isEmpty(sort)) sort = "1";

            List<Item> items = itemMapper.selectAllItem(
                    request.getCid(), Integer.parseInt(sort),
                    request.getPriceGt(),
                    request.getPriceLte());
            log.info("getAllProduct-2nd 分页查询商品列表 正常");

            PageInfo<Item> info = new PageInfo<>(items);

            response.setTotal(info.getTotal());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            // 数据转换
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            response.setData(productDtos);
            log.info("分页查询商品列表 结束 no error ");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("分页查询 数据 异常");
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return response;
    }

    /**
     * <h2> 查询推荐商品 </h2>
     * Created by GaoJia
     */
    @Override
    public RecommendResponse getRecommendGoods() {
        log.info("调用 getRecommendGoods 查询推荐商品");
        RecommendResponse response = new RecommendResponse();
        int panelId = GlobalConstants.RECOMMEND_PANEL_ID;

        try {
            Panel panel = panelMapper.selectByPrimaryKey(panelId);
            List<PanelContentItem> panelContentItems =
                    panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);
            log.info("查询 推荐商品 正常");

            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

            // 理顺 数据转换的顺序
            // PanelContentItemDto 给 PanelDto 中的 panelContentItems
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);

            // 将 panelDto 放入 response 中
            Set<PanelDto> panelDtoSet = new HashSet<>();
            panelDtoSet.add(panelDto);
            response.setPanelContentItemDtos(panelDtoSet);
            log.info("查询推荐商品 结束 no error ");
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }




}
